package managedBean;

import Merlion.MRP.bean.DemandManagementBeanRemote;
import Merlion.util.state.SalesRecordState;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.event.PhaseEvent;
import org.primefaces.event.RowEditEvent;

@ManagedBean(name = "SalesForecastManagedBean")
@ViewScoped
public class SalesForecastManagedBean {

    private Integer startMonth;
    private Integer endMonth;
    private Integer startYear;
    private Integer endYear;
    private int yearOnYearGrowth;
    private List<SalesRecordState> forecastMonths;
    private SalesRecordState[] forecastMonthss;
    private SalesRecordState forecastMonth;
    private int previousSales;
    private int previousPromotion;
    private int previousBase;
    private int growth;
    private int growthRate;
    private int projectedBase;
    private int projectedPromotion;
    private int projectedSales;
    private int monthInt;
    private List<SalesRecordState> selectedForecastMonths;
    @EJB
    private DemandManagementBeanRemote dmb;

    public SalesForecastManagedBean() {
    }

    public void initView(PhaseEvent event) {
        System.out.println("**SalesForecastManagedBean: initView()");

        FacesContext.getCurrentInstance().getExternalContext().getSession(true);
        if (selectedForecastMonths == null) {
            //selectedForecastMonths = dmb.forecastSales(startYear, startMonth, endYear, endMonth, yearOnYearGrowth);
        }
    }

    public void doFinalize(ActionEvent event) {
        System.out.println("**SalesForecastManagedBean: doFinalize()");

        try {
            if (forecastMonthss != null) {
                for (SalesRecordState srs : forecastMonthss) {
                    int yearInt = srs.getYearInt();
                    int monthIn = srs.getMonthInt();
                   
                    dmb.finalizeSingleMonthlyForecast(yearInt, monthIn);
                    FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Successful", "Monthly Forecast for month " + monthIn + " and year " + yearInt + " has been finalized"));
                    this.doForecastSales(event);
                    
                }
            } else {
                System.out.println("**SalesForecastManagedBean: Selected forecast months are null.");
            }

        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public void doDefinalize(ActionEvent event) {
        System.out.println("**SalesForecastManagedBean : doDefinalize()");

        try {
            if (forecastMonthss != null) {
                for (SalesRecordState srs : forecastMonthss) {
                    int yearInt = srs.getYearInt();
                    int monthIn = srs.getMonthInt();
                    dmb.deFinalizeSingleMonthlyForecast(yearInt, monthIn);
                    FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Successful", "Monthly Forecast for month " + monthIn + " and year " + yearInt + " has been definalized"));
                    this.doForecastSales(event);
                }
            } else {
                System.out.println("**SalesForecastManagedBean: Selected forecast months are null.");
            }

        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public void doForecastSales(ActionEvent event) {
        System.out.println("**SalesForecastManagedBean: doForecastSales()");

        forecastMonths = dmb.forecastSales(startYear, startMonth, endYear, endMonth, yearOnYearGrowth);
        this.setSelectedForecastMonths(forecastMonths);
    }

    public void doRefresh(ActionEvent event) {
        try {
            FacesContext.getCurrentInstance().getExternalContext().redirect("SalesForecast.xhtml");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void onEditRow(RowEditEvent event) {
        System.out.println("**SalesForecastManagedBean: onEditRow()");

        try {
            SalesRecordState sr = (SalesRecordState) event.getObject();
            if (sr != null) {
                int eMonthInt = sr.getMonthInt();
                int eYearInt = sr.getYearInt();
                int eGrowth = this.getYearOnYearGrowth();
                int ePromotion = sr.getProjectedPromotion();
                forecastMonth = dmb.updateSingleMonthlyForecast(eYearInt, eMonthInt, eGrowth, ePromotion);
                
                sr.setProjectedSales(forecastMonth.getProjectedSales());
                System.out.println("project sales in managed bean is " + forecastMonth.getProjectedSales());
                System.out.println("**SalesForecastManagedBean: successful edit");
            } else {
                System.out.println("**SalesForecastManagedBean: fail to edit because sales record is null");
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public Integer getStartMonth() {
        return startMonth;
    }

    public void setStartMonth(Integer startMonth) {
        this.startMonth = startMonth;
    }

    public Integer getEndMonth() {
        return endMonth;
    }

    public void setEndMonth(Integer endMonth) {
        this.endMonth = endMonth;
    }

    public Integer getStartYear() {
        return startYear;
    }

    public void setStartYear(Integer startYear) {
        this.startYear = startYear;
    }

    public Integer getEndYear() {
        return endYear;
    }

    public void setEndYear(Integer endYear) {
        this.endYear = endYear;
    }

    public int getYearOnYearGrowth() {
        return yearOnYearGrowth;
    }

    public void setYearOnYearGrowth(int yearOnYearGrowth) {
        this.yearOnYearGrowth = yearOnYearGrowth;
    }

    public List<SalesRecordState> getForecastMonths() {
        return forecastMonths;
    }

    public void setForecastMonths(List<SalesRecordState> forecastMonths) {
        this.forecastMonths = forecastMonths;
    }

    public int getPreviousSales() {
        return previousSales;
    }

    public void setPreviousSales(int previousSales) {
        this.previousSales = previousSales;
    }

    public int getPreviousPromotion() {
        return previousPromotion;
    }

    public void setPreviousPromotion(int previousPromotion) {
        this.previousPromotion = previousPromotion;
    }

    public int getGrowthRate() {
        return growthRate;
    }

    public void setGrowthRate(int growthRate) {
        this.growthRate = growthRate;
    }

    public int getPreviousBase() {
        return previousBase;
    }

    public void setPreviousBase(int previousBase) {
        this.previousBase = previousBase;
    }

    public int getGrowth() {
        return growth;
    }

    public void setGrowth(int growth) {
        this.growth = growth;
    }

    public int getProjectedBase() {
        return projectedBase;
    }

    public void setProjectedBase(int projectedBase) {
        this.projectedBase = projectedBase;
    }

    public int getProjectedPromotion() {
        return projectedPromotion;
    }

    public void setProjectedPromotion(int projectedPromotion) {
        this.projectedPromotion = projectedPromotion;
    }

    public int getProjectedSales() {
        return projectedSales;
    }

    public void setProjectedSales(int projectedSales) {
        this.projectedSales = projectedSales;
    }

    public int getMonthInt() {
        return monthInt;
    }

    public void setMonthInt(int monthInt) {
        this.monthInt = monthInt;
    }

    public List<SalesRecordState> getSelectedForecastMonths() {
        return selectedForecastMonths;
    }

    public void setSelectedForecastMonths(List<SalesRecordState> selectedForecastMonths) {
        this.selectedForecastMonths = selectedForecastMonths;
    }

    public DemandManagementBeanRemote getDmb() {
        return dmb;
    }

    public void setDmb(DemandManagementBeanRemote dmb) {
        this.dmb = dmb;
    }

    public SalesRecordState[] getForecastMonthss() {
        return forecastMonthss;
    }

    public void setForecastMonthss(SalesRecordState[] forecastMonthss) {
        this.forecastMonthss = forecastMonthss;
    }

    public SalesRecordState getForecastMonth() {
        return forecastMonth;
    }

    public void setForecastMonth(SalesRecordState forecastMonth) {
        this.forecastMonth = forecastMonth;
    }
}
